package com.example.demo.controller;

import com.alibaba.excel.EasyExcel;
import com.example.demo.ao.Attendance;
import com.example.demo.mapper.AttendanceMapper;
import com.example.demo.utils.AttendanceDataListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/attendance")
@Slf4j
public class AttendanceController {

    private final AttendanceMapper attendanceMapper;

    public AttendanceController(AttendanceMapper attendanceMapper) {
        this.attendanceMapper = attendanceMapper;
    }

    @PostMapping("/import")
    public String importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), Attendance.class,
                        new AttendanceDataListener(attendanceMapper))
                .sheet("每日统计")  // 指定sheet名称
                .headRowNumber(3)
                .doRead();
        return "导入成功";

    }
}